package com.shiro.shiroweb.controller;


import com.shiro.shiroweb.entity.UserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @PostMapping("/login")
    public String login(UserEntity user,Boolean isRemember){
        try{
            Subject subject= SecurityUtils.getSubject();
            UsernamePasswordToken token=new UsernamePasswordToken(user.getName(),user.getPassword());
            System.out.println(token.toString());
            token.setRememberMe(isRemember);
            subject.login(token);
            return "login";
        }catch (AuthorizationException e){
            return "fail";
        }

    }
    //@RequiresRoles("admin")
    @GetMapping("/testRole")
    public String testRole(){
        return "You're administrator.";
    }

    //@RequiresRoles("user:select")
    @GetMapping("/testPermission")
    public String testPermission(){
        return "You have this permission.";
    }
}
